package cn.stylefeng.guns.sys.modular.notice.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.notice.entity.DbNotice;
import cn.stylefeng.guns.sys.modular.notice.mapper.DbNoticeMapper;
import cn.stylefeng.guns.sys.modular.notice.model.params.DbNoticeParam;
import cn.stylefeng.guns.sys.modular.notice.model.result.DbNoticeResult;
import cn.stylefeng.guns.sys.modular.notice.service.DbNoticeService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 通知中心  服务实现类
 * </p>
 *
 * @author author
 * @since 2020-08-19
 */
@Service
public class DbNoticeServiceImpl extends ServiceImpl<DbNoticeMapper, DbNotice> implements DbNoticeService {

    @Override
    public void add(DbNoticeParam param) {
        DbNotice entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(DbNoticeParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(DbNoticeParam param) {
        DbNotice oldEntity = getOldEntity(param);
        DbNotice newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DbNoticeResult findBySpec(DbNoticeParam param) {
        return null;
    }

    @Override
    public List<DbNoticeResult> findListBySpec(DbNoticeParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DbNoticeParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public DbNoticeResult findByParam(DbNoticeParam param) {
        return this.baseMapper.searchByParam(param);
    }

    @Override
    public Integer updateInfoByServiceId(DbNoticeParam dbNoticeParam) {
        return this.baseMapper.updateByServiceId(dbNoticeParam);
    }
















    private Serializable getKey(DbNoticeParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private DbNotice getOldEntity(DbNoticeParam param) {
        return this.getById(getKey(param));
    }

    private DbNotice getEntity(DbNoticeParam param) {
        DbNotice entity = new DbNotice();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
